package com.galaxy.product.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.galaxy.common.core.domain.R;
import com.galaxy.common.core.validate.AddGroup;
import com.galaxy.common.core.validate.EditGroup;
import com.galaxy.common.log.annotation.Log;
import com.galaxy.common.log.enums.BusinessType;
import com.galaxy.common.mybatis.core.page.PageQuery;
import com.galaxy.common.mybatis.core.page.TableDataInfo;
import com.galaxy.product.domain.ProductUnit;
import com.galaxy.product.domain.bo.ProductUnitBo;
import com.galaxy.product.domain.vo.ProductUnitVo;
import com.galaxy.product.service.IProductUnitService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品单位Controller
 */
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/unit")
public class ProductUnitController {

    private final IProductUnitService productUnitService;

    /**
     * 查询商品单位列表
     */
    @GetMapping("/list")
    @SaCheckPermission("product:unit:list")
    public TableDataInfo<ProductUnitVo> list( ProductUnitBo bo, PageQuery pageQuery) {
        return productUnitService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取商品单位详细信息
     */
    @GetMapping("/{id}")
    @SaCheckPermission("product:unit:list")
    public R<ProductUnit> getInfo(@PathVariable Long id) {
        return R.ok(productUnitService.getInfo(id));
    }

    /**
     * 新增商品单位
     */
    @Log(title = "商品单位管理", businessType = BusinessType.INSERT)
    @SaCheckPermission("product:unit:add")
    @PostMapping
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ProductUnitBo bo) {
        productUnitService.add(bo);
        return R.ok();
    }

    /**
     * 修改商品单位
     */
    @Log(title = "商品单位管理", businessType = BusinessType.UPDATE)
    @SaCheckPermission("product:unit:update")
    @PutMapping
    public R<Void> update(@Validated(EditGroup.class) @RequestBody ProductUnitBo bo) {
        productUnitService.update(bo);
        return R.ok();
    }

    /**
     * 删除商品单位
     */
    @Log(title = "商品单位管理", businessType = BusinessType.DELETE)
    @SaCheckPermission("product:unit:delete")
    @DeleteMapping("/{id}")
    public R<Void> delete(@PathVariable Long id) {
        productUnitService.delete(id);
        return R.ok();
    }

    /**
     * 批量删除商品单位
     */
    @Log(title = "商品单位管理", businessType = BusinessType.DELETE)
    @SaCheckPermission("product:unit:delete")
    @DeleteMapping("/batch/{ids}")
    public R<Void> deleteBatch(@PathVariable Long[] ids) {
        productUnitService.deleteBatch(ids);
        return R.ok();
    }
}
